package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.entity.PageResult;
import com.bnz.entity.R;
import com.bnz.entity.Student;
import com.bnz.entity.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
import org.apache.commons.lang3.StringUtils;

import javax.jws.WebMethod;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;

    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决post请求的中文乱码
        req.setCharacterEncoding("UTF-8");
        // 0. 设置相应内容及字符集编码，将查询的内容送到前端页面上
        resp.setContentType("text/html;charset=utf8");
        // 1. 得到查询参数cmd的值
        String cmd = req.getParameter("cmd");
        // 2. 根据cmd的参数区分执行不同的方法
        if (StringUtils.isNotBlank(cmd)){
            // 2.1 分页查询
            if ("findByPage".equals(cmd)){
                findByPage(req,resp);
            }
            // 2.2 列表查询
            if ("list".equals(cmd)){
                list(req,resp);
            }
            // 2.3 条件查询带着分页
            if ("search".equals(cmd)){
                search(req,resp);
            }
            // 2.4添加学生
            if("add".equals(cmd)){
                add(req,resp);
            }
            // 2.4修改学生
            if("update".equals(cmd)){
                update(req,resp);
            }
            // 2.5 删除学生
            if("delete".equals(cmd)){
                delete(req,resp);
            }
        }
    }


    // 2. 分页查询
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //2.1 得到分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //2.2 将字符串转换为整数类型
        int page = Integer.parseInt(pageStr);
        int pageSize =  Integer.parseInt(pageSizeStr);
        //2.3 查询当前页的数据
        PageResult<Student> pr = studentService.findByPage(page,pageSize);
        //2.4 将pr对象放在r对象中
        R r = R.ok().put("pr", pr);
        //2.5 将r对象转换为字符串
        String s = JSON.toJSONString(r);
        //2.6 输出到页面中
        resp.getWriter().println(s);
        //2.7 刷新
        resp.getWriter().flush();
    }
    // 3. 列表查询
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 3.1 查询所有学生列表
        List<Student> students = studentService.findAll();
        // 3.2 将学生放到R对象中
        R data = R.ok().put("list", students);
        // 3.3 将R对象转换为字符串
        String s = JSON.toJSONString(data);
        // 3.4 输出到页面中
        resp.getWriter().println(s);
        // 3.5 刷新
        resp.getWriter().flush();
    }
    // 4. 条件查询带着分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //4.1 得到分页参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //4.2 将字符串转换为整数类型
        int page = Integer.parseInt(pageStr);
        int pageSize =  Integer.parseInt(pageSizeStr);
        // 4.3 得到条件查询对象
        String stud = req.getParameter("stud");
        // 4.4 将这个对象转换为Java对象
        StudentVo studentVo = JSON.parseObject(stud, StudentVo.class);
        // 4.5 开始查询
        PageResult<Student> pr = studentService.search(page,pageSize,studentVo);
        // 4.6 将pr对象放在r对象中
        R r = R.ok().put("pr", pr);
        // 4.7 将r对象转换为字符串
        String s = JSON.toJSONString(r);
        // 4.8 输出到页面中
        resp.getWriter().println(s);
        // 4.9 刷新
        resp.getWriter().flush();
    }
    //5. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //5.1 得到后台传入的数据
        String stud = req.getParameter("stud");
        //5.2 转换为学生对象
        Student student = JSON.parseObject(stud,Student.class);
        //5.3 保存学生对象
        studentService.update(student);
        R r = R.ok().put("message", "修改学生成功！");
        //5.4 保存到数据库
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }
    //6. 添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //6.1 得到后台传入的数据
        String stud = req.getParameter("stud");
        //6.2 转换为学生对象
        Student student = JSON.parseObject(stud,Student.class);
        //6.3 保存学生对象
        studentService.save(student);
        R r = R.ok().put("message", "添加学生成功！");
        //6.4 保存到数据库
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }
    //7. 根据学号删除学生
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //7.1 得到要删除的学号
        String sids = req.getParameter("sids");
        System.out.println("sids = " + sids);
        //7.2 将学生编号包装到集合中
        List<String> ids = new ArrayList<>();
        String[] split = sids.split(",");
        if(split.length == 0){
            ids.add(sids);
        }else{
            ids = Arrays.asList(split);
        }
        //7.3 根据集合删除学生
        studentService.delete(ids);
        R r = R.ok().put("message", "删除学生成功！");
        //7.4 保存到数据库
        resp.getWriter().println(JSON.toJSONString(r));
        resp.getWriter().flush();
    }

}
